package eos.dom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import eos.dom.client.DomCustomArrayAdapter;
import eos.dom.client.DomSAXHandler;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Xml;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.GridLayoutAnimationController;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;

public class DomGame extends Activity {
	
	private Handler mHandler = new Handler();
	public static int lol = 0;
	public GameInfo gameInfo;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gamelayout);
		gameInfo = new GameInfo();
		
		//updates the activity every 2 sec
		Thread updater = new Thread(new Runnable() {
			@Override
			public void run() {
					updateActivity();
					mHandler.postDelayed(this, 2000);
			}
		});
		
		mHandler.removeCallbacks(updater);
        mHandler.postDelayed(updater, 100);

		Button start = (Button)findViewById(R.id.StartGameButton);
		start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DomStart.client.start();
				Button endTurn = (Button)findViewById(R.id.EndTurnButton);
				endTurn.setEnabled(true);
				v.setEnabled(false);
			}
		});
		if(DomStart.client.getName().equals(DomStart.client.getGame())){
			start.setEnabled(true);
		}else{
			start.setEnabled(false);
		}

		Button endTurn = (Button)findViewById(R.id.EndTurnButton);
		endTurn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		endTurn.setEnabled(false);

		Button quit = (Button)findViewById(R.id.QuitButton);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		
		firstUpdateActivity();

	}

	private void firstUpdateActivity(){
		String info = "";
		try {
			info = DomStart.client.getUpdateInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
		DomSAXHandler handler = new DomSAXHandler(this);
		try {
			Xml.parse(info, handler);
		} catch (SAXException e) {
			e.printStackTrace();
		}
		GridView kGrid = (GridView)findViewById(R.id.KingdomCardsLayout);
		DomCustomArrayAdapter a = new DomCustomArrayAdapter(this, 0);
		kGrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long arg3) {
				CardId id = ((DomCardStack)parent.getAdapter().getItem(pos)).getId();
				Dialog image = new Dialog(DomGame.this);
				ImageView im = new ImageView(DomGame.this);
				im.setImageResource(DomCustomArrayAdapter.bigCards[id.ordinal()]);
				image.setContentView(im);
				image.show();
			}
		});
		//Kingdom
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(0)], gameInfo.getkNum().get(0)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(1)], gameInfo.getkNum().get(1)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(2)], gameInfo.getkNum().get(2)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(3)], gameInfo.getkNum().get(3)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(4)], gameInfo.getkNum().get(4)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(5)], gameInfo.getkNum().get(5)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(6)], gameInfo.getkNum().get(6)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(7)], gameInfo.getkNum().get(7)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(8)], gameInfo.getkNum().get(8)));
		a.add(new DomCardStack(CardId.class.getEnumConstants()[gameInfo.getK().get(9)], gameInfo.getkNum().get(9)));
		a.add(new DomCardStack(CardId.ESTATE, gameInfo.getEstate()));
		a.add(new DomCardStack(CardId.DUCHY, gameInfo.getDuchy()));
		a.add(new DomCardStack(CardId.PROVINCE, gameInfo.getProvince()));
		a.add(new DomCardStack(CardId.CURSE, gameInfo.getCurse()));
		a.add(new DomCardStack(CardId.COPPER, gameInfo.getCopper()));
		a.add(new DomCardStack(CardId.SILVER, gameInfo.getSilver()));
		a.add(new DomCardStack(CardId.GOLD, gameInfo.getGold()));
		a.add(new DomCardStack(CardId.POTION, gameInfo.getPotion()));
		kGrid.setAdapter(a);
		
		//Hand of the player
		LinearLayout hand = (LinearLayout)findViewById(R.id.HandCardsLinearLayout);
		hand.removeAllViews();
		for(int c:gameInfo.getHand()){
			ImageView im = new ImageView(this);
			im.setImageResource(DomCustomArrayAdapter.cards[c]);
			hand.addView(im);
		}
		
	}
	
	private void updateActivity(){
		/*String info = "";
		try {
			info = DomStart.client.getUpdateInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
		DomSAXHandler handler = new DomSAXHandler(this);
		try {
			Xml.parse(info, handler);
		} catch (SAXException e) {
			e.printStackTrace();
		}
		*/
	}

	@Override
	public void onBackPressed() {
		//Affiche un popup pour demander confirmation
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to quit the game?\nThe game will end for everyone! :(")
		.setCancelable(true)
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				try {
					DomStart.client.endGame();
					DomGame.this.finish();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		builder.show();
	}
}
